import requests
from bs4 import BeautifulSoup
import csv

all_info = []
for num in range(1,40):
    url = 'https://hf.lianjia.com/zufang/pg' + str(num)
    try:
        html = requests.get(url,headers={'user-agent':'chrome'})
    #print(html.text)
    except:
        print('获取网页失败')
    print('正在抓取第%d页' % num)
    soup = BeautifulSoup(html.text,'html.parser')
        
    content = soup.find_all(class_ = 'content__list--item--main')
    #print(content)
    #print(type(content))
    for i in range(len(content)):
        rent_info = []
        title = soup.find_all(class_ = 'content__list--item--title twoline')
        #print(title[i].get_text(strip=True))
        link = title[i].a['href']
        rent_info.append(link)
        rent_info.append(title[i].get_text(strip=True))
        
        #print(rent_info)
        des = soup.find_all(class_ = 'content__list--item--des')
        #print(des.get_text(strip=True))
        area = des[i].get_text(strip=True)
        rent_info.append(''.join(area.split()))
        #print(rent_info)
        decoration = soup.find_all(class_ = 'content__list--item--bottom oneline')
        rent_info.append(decoration[i].get_text(strip=True))
        price = soup.find_all(class_ = 'content__list--item-price')
        rent_info.append(price[i].get_text(strip=True))
        all_info.append(rent_info)
#print(all_info)

try:
    with open('爬虫\\租房信息.csv','w',encoding='utf8',newline='') as f:
        f_csv = csv.writer(f)
        for i in all_info:
            f_csv.writerow(i)
except:
    print('写入失败')
